package com.laoniu.biz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.laoniu.dto.PageResponse;
import com.laoniu.dto.request.RoleFormDto;
import com.laoniu.dto.request.RoleRequestDto;
import com.laoniu.dto.response.RoleResponseDto;
import com.laoniu.entity.SecurityUser;
import com.laoniu.entity.SysRole;
import com.laoniu.entity.SysRoleMenu;
import com.laoniu.service.sys.RoleMenuService;
import com.laoniu.service.sys.RoleService;
import com.laoniu.utils.LocalDateTimeUtils;
import com.laoniu.utils.SpringSecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
public class RoleBiz {

    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMenuService roleMenuService;

    /**
     *  分页查询
     * @param requestDto
     * @return
     */
    public PageResponse<RoleResponseDto> list(RoleRequestDto requestDto){
        Page<SysRole> page = roleService.pageList(requestDto);
        PageResponse<RoleResponseDto> result = new PageResponse<RoleResponseDto>();
        result.setTotal(page.getTotal());
        result.setPageNo(requestDto.getPageNo());
        result.setPageSize(requestDto.getPageSize());
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            List<RoleResponseDto> resultList = Lists.newArrayList();
            for (SysRole record : page.getRecords()) {
                RoleResponseDto responseDto = convertDto(record);
                resultList.add(responseDto);
            }
            result.setData(resultList);
        }
        return result;
    }

    /**
     * 添加角色
     * @param dto
     */
    public void addRole(RoleFormDto dto){
        SecurityUser user = SpringSecurityUtils.getUser();
        SysRole role = new SysRole();
        role.setCreateTime(LocalDateTime.now());
        role.setName(dto.getName());
        role.setDescription(dto.getDescription());
        role.setOperateUserId(user.getId());
        role.setOperateUserName(user.getName());
        roleService.save(role);
    }

    /**
     * 修改角色
     * @param dto
     */
    public void updateRole(RoleFormDto dto){
        SecurityUser user = SpringSecurityUtils.getUser();
        SysRole role = roleService.getById(dto.getId());
        if (Objects.isNull(role)){
            return;
        }
        role.setName(dto.getName());
        role.setDescription(dto.getDescription());
        role.setOperateUserId(user.getId());
        role.setOperateUserName(user.getName());
        roleService.updateById(role);
    }

    /**
     * 删除角色
     * @param dto
     */
    public void deleteById(RoleFormDto dto){
        roleService.removeById(dto.getId());
        //删除绑定的菜单
        roleMenuService.deleteByRoleId(dto.getId());
    }

    /**
     * 转换为dto
     * @param role
     * @return
     */
    private RoleResponseDto convertDto(SysRole role){
        RoleResponseDto dto = new RoleResponseDto();
        dto.setCreateTime(LocalDateTimeUtils.localDateTimeToLong(role.getCreateTime()));
        dto.setName(role.getName());
        dto.setDescription(role.getDescription());
        dto.setId(role.getId());
        dto.setOperateUserId(role.getOperateUserId());
        dto.setOperateUserName(role.getOperateUserName());
        return dto;
    }

    /**
     * 获取角色所分配的菜单id
     * @param roleId
     * @return
     */
    public List<Long> getRoleMenuIds(Long roleId) {
        List<SysRoleMenu> roleMenus = roleMenuService.selectMenuListByRoleId(roleId);
        if (CollectionUtils.isEmpty(roleMenus)){
            return Lists.newArrayList();
        }
        return roleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
    }

    /**
     * 保存角色权限
     * @param dto
     */
    public void saveRoleMenu(RoleFormDto dto) {
        if (Objects.isNull(dto.getId()) || CollectionUtils.isEmpty(dto.getMenuIds())){
            return;
        }
        SecurityUser user = SpringSecurityUtils.getUser();
        List<SysRoleMenu> roleMenuList = Lists.newArrayList();
        for (Long menuId : dto.getMenuIds()) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(dto.getId());
            roleMenu.setOpTime(LocalDateTime.now());
            roleMenu.setOpId(user.getId());
            roleMenu.setOpName(user.getName());
            roleMenuList.add(roleMenu);
        }
        //删除现有的
        roleMenuService.deleteByRoleId(dto.getId());
        //重新添加新的
        roleMenuService.saveBatch(roleMenuList);
    }

    public List<RoleResponseDto> roleList() {
        List<SysRole> roleList = roleService.list();
        if (CollectionUtils.isEmpty(roleList)){
            return Lists.newArrayList();
        }
        List<RoleResponseDto> result = Lists.newArrayList();
        for (SysRole role : roleList) {
            RoleResponseDto dto = convertDto(role);
            result.add(dto);
        }
        return result;
    }
}
